//
//  AccountManager.m
//  easymarketing
//
//  Created by HailongHan on 14/12/24.
//  Copyright (c) 2014年 cubead. All rights reserved.
//

#define AccountFilepath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"user.data"]

#define Free_Account 1
#define Charge_Account 0

#import "AccountManager.h"

@implementation AccountManager

+(AccountManager *)manager{
    static AccountManager *account_manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        account_manager = [[AccountManager alloc] init];
    });
    return account_manager;
}

/**
 *  归档账户信息
 *
 *  @param account LoginResponse
 */
- (void)saveAccount:(LoginDataResp *)account{
    //归档
    [NSKeyedArchiver archiveRootObject:account toFile:AccountFilepath];
    CMLog(@"归档成功");
}

/**
 *  获取账号信息
 *
 *  @return 账号信息
 */
- (LoginDataResp *)account{
    //反归档
    LoginDataResp *account = [NSKeyedUnarchiver unarchiveObjectWithFile:AccountFilepath];
    return account;
}

/**
 *  获取token信息
 *
 *  @return token令牌
 */
- (NSString *)token{
    LoginDataResp *account = [self account];
    if (account == nil) {
        return nil;
    }
    return account.token;
}

/**
 *  清除账号信息
 */
- (void)removeAccount{
    NSFileManager *defaultManager = [NSFileManager defaultManager];
    if ([defaultManager isDeletableFileAtPath:AccountFilepath]) {
        [defaultManager removeItemAtPath:AccountFilepath error:nil];
    }
}


-(void)logoutAndClearUserInfo{
    //清除登陆信息
    [self removeAccount];
}

@end
